package com.isoft.uaaservice.handler;



import com.isoft.uaaservice.bean.ResponseData;
import com.isoft.uaaservice.bean.ResultCode;
import com.isoft.uaaservice.bean.exp.TokenException;
import com.isoft.uaaservice.bean.exp.UserNonLoginException;
import com.isoft.uaaservice.bean.exp.VerifyCodeException;
import com.isoft.uaaservice.util.WebUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        log.error("AuthenticationEntryPointImpl: " + authException.getClass().getName() + "--" + authException.getMessage());

        ResponseData responseData = new ResponseData();
        if (authException instanceof TokenException){
            responseData.setErrorCode(ResultCode.TOKEN_INVALID.getCode());
            responseData.setMsg(ResultCode.TOKEN_INVALID.getMessage());
        } else if (authException instanceof VerifyCodeException) {
            responseData.setErrorCode(ResultCode.VALIDCODE_INVALID.getCode());
            responseData.setMsg(ResultCode.VALIDCODE_INVALID.getMessage());
        } else if (authException instanceof UserNonLoginException) {
            responseData.setErrorCode(ResultCode.USER_NOT_LOGIN.getCode());
            responseData.setMsg(ResultCode.USER_NOT_LOGIN.getMessage());
        } else if (authException instanceof BadCredentialsException) {
            responseData.setErrorCode(ResultCode.USER_CREDENTIALS_ERROR.getCode());
            responseData.setMsg(ResultCode.USER_CREDENTIALS_ERROR.getMessage());
        } else if (authException instanceof LockedException) {
            responseData.setErrorCode(ResultCode.USER_ACCOUNT_LOCKED.getCode());
            responseData.setMsg(ResultCode.USER_ACCOUNT_LOCKED.getMessage());
        } else if (authException instanceof InternalAuthenticationServiceException) {
            responseData.setErrorCode(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode());
            responseData.setMsg(ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }else {
            responseData.setErrorCode(ResultCode.NO_PERMISSION.getCode());
            responseData.setMsg(ResultCode.NO_PERMISSION.getMessage());
        }
        WebUtils.renderObj(response , responseData);
    }
}
